﻿using Azrng.DbOperator;
using Common.Core.Helpers;
using Common.Extension;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using DBCHM.Models;
using DBCHM.Service;
using DBCHM.Utils.Events;
using Irihi.Avalonia.Shared.Contracts;
using System;
using System.Threading.Tasks;
using Ursa.Controls;

namespace DBCHM.ViewModels;

public partial class AddConnectWindowViewModel : ViewModelBase, IDialogContext
{
    private readonly IMessageService _messageService;
    private readonly IStoreService _storeService;
    private readonly IBusinessService _businessService;

    public AddConnectWindowViewModel(IMessageService messageService, IStoreService storeService, IBusinessService businessService)
    {
        _messageService = messageService;
        _storeService = storeService;
        _businessService = businessService;
        ConnectConfigDto = new DbConnectConfigDto();
        DbTypeEnum = typeof(DatabaseType);
    }

    public void Close()
    {
        RequestClose?.Invoke(this, null);
    }

    public event EventHandler<object?>? RequestClose;

    /// <summary>
    /// 连接配置
    /// </summary>
    [ObservableProperty]
    private DbConnectConfigDto _connectConfigDto;

    /// <summary>
    /// 数据库类型枚举
    /// </summary>
    [ObservableProperty]
    private Type _dbTypeEnum;

    [RelayCommand]
    private async Task AddData()
    {
        try
        {
            if (ConnectConfigDto.ConnectName.IsNullOrWhiteSpace())
            {
                _messageService.SendMessage("连接名称不能为空");
                return;
            }

            if (ConnectConfigDto.Host.IsNullOrWhiteSpace())
            {
                _messageService.SendMessage("主机地址不能为空");
                return;
            }

            if (ConnectConfigDto.Port <= 0)
            {
                _messageService.SendMessage("端口号不能为空");
                return;
            }

            if (ConnectConfigDto.DataBase.IsNullOrWhiteSpace())
            {
                _messageService.SendMessage("数据库名称不能为空");
                return;
            }

            if (ConnectConfigDto.UserName.IsNullOrWhiteSpace())
            {
                _messageService.SendMessage("用户名不能为空");
                return;
            }

            if (ConnectConfigDto.Password.IsNullOrWhiteSpace())
            {
                _messageService.SendMessage("密码不能为空");
                return;
            }

            if (ConnectConfigDto.Timeout < 0)
            {
                _messageService.SendMessage("超时时间不能小于0");
                return;
            }

            // 连接测试

            var flag = await _businessService.ConnectTestAsync(ConnectConfigDto);
            if (!flag)
            {
                _messageService.SendMessage("连接测试失败");
                return;
            }

            await _storeService.AddConfigAsync(ConnectConfigDto);
            _messageService.SendMessage("添加成功");

            RequestClose?.Invoke(this, DialogResult.OK);
        }
        catch (Exception e)
        {
            LocalLogHelper.WriteMyLogs("ERROR", e.Message);
            _messageService.SendMessage("添加失败");
        }
    }

    [RelayCommand]
    private void Cancel()
    {
        RequestClose?.Invoke(this, DialogResult.No);
    }
}